/**
* 按小时、分浏览器输出log分析结果
* 自动获取服务器时间，分析当前小时的dat文件
* usage : dc_analysis log_path
* author: shaomin.shi@
*/
#include<stdio.h>
#include<iostream>
#include<fstream>
#include<vector>
#include<iterator>
#include<string>
#include "clx/filesystem.h"
#include "clx/format.h"
#include "clx/time.h"

#define MAX_LINE_SIZE 	1024
#define MAX_BROWSER_NUM 100
#define MAX_STEP_NUM 	900

using namespace std;

struct result{
	string browser;
	int step[MAX_STEP_NUM];
	unsigned count;
}log_result[MAX_BROWSER_NUM];

string get_line_index_data(string str,int index);

int main( int argc ,char *argv[])
{
	if(argc<2)
	{
		cout<<"usage:# dc_analysis log_path "<<endl;
		return 0;
	}
	
	string path( argv[1] );
	vector<string> v;
	clx::filesystem::list(path,back_inserter(v));
	for(vector<string>::iterator pos = v.begin(); pos!=v.end(); ++pos)
	{
		string filename = *pos;
		if(filename.at(filename.length()-1)=='/'){
			filename = filename.substr(0,filename.length()-1);
		} 
		if(filename.length()>4 && (filename.substr(filename.length()-4,4).compare(".dat")==0))
		{
			//当前小时
			string fmt = "%Y%m%d%H";
			clx::date_time now;
			string now_str = now.to_string<char>(fmt);
			if(filename.find(now_str)==string::npos){
				continue;
			}
			char buffer[MAX_LINE_SIZE];
			vector<string> v_log;
			vector<string>::iterator it_log;
			string full_name = path + filename;
			ifstream in(full_name.c_str());
			if(in.is_open())
			{
				while( !in.eof() )
				{
					in.getline(buffer,MAX_LINE_SIZE);
					v_log.push_back(buffer);
				}
				in.close();
			}
			for(it_log=v_log.begin();it_log!=v_log.end();++it_log)
			{
				string log_time = get_line_index_data(*it_log, 1);
				string log_step = get_line_index_data(*it_log, 4);
				string log_browser = get_line_index_data(*it_log, 8);
				if(log_browser.substr(0,4).compare("msie")==0)
				{
					int npos = log_browser.find(";");
					if(npos>0){
						log_browser = log_browser.substr(0,npos);
					}
				}else{
					int npos = log_browser.find("/");
					if(npos>0){
						log_browser = log_browser.substr(0,npos);
					}
				}
				int found = 0;
				for(int i=0;i<MAX_BROWSER_NUM;i++)
				{
					if( log_result[i].browser.length()==0 ) break;
					if( log_result[i].browser.compare(log_browser) == 0) break;
					found++;
				}
				log_result[found].browser = log_browser;
				int step = std::atoi(log_step.c_str());
				log_result[found].step[step]++;
				log_result[found].count++;
				//cout<<step<<endl;
				//cout<<log_time<<"-"<<log_step<<"-"<<log_browser<<endl;
			}
		}
	}
	cout<<"step\tcount\tratio"<<endl;
	for(int i=0;i<MAX_BROWSER_NUM;i++)
	{	
		if(log_result[i].browser.length()>0)
		{
			cout<<log_result[i].browser<<":"<<endl;	
			for(int j=0;j<MAX_STEP_NUM;j++)
			{	
				if(log_result[i].step[20]==0) log_result[i].step[20] = 1;
				float base_num = log_result[i].step[20];
				float rate = (float)log_result[i].step[j]/(float)base_num;
				cout.setf(ios::fixed);
				cout.precision(4);
				if(log_result[i].step[j]>0){
					cout<<j<<"\t"<<log_result[i].step[j]<<"\t"<<rate<<endl;
				}
			}
		}
	}
	
	return 0;
}

string get_line_index_data(string str,int index)
{
	int pos=0,len=0;
	for(int i=0;i<(int)str.length();i++)
	{
		if(str.at(i)=='\t'){
			if(index==pos)
			{
				return str.substr(len+1,i-len-1);
			}
			len = i;
			pos++;
		}
	}
	return "";
}










